﻿using Hue_Demo_Phone.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Hue_Demo_WinStore
{
    public sealed partial class MainPage
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.DataContext = App.HueClientVM;
            
        }

        HueClientViewModel _viewModel { get { return (HueClientViewModel)this.DataContext; } }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            _viewModel.AnimateViewModel.RunReset();
        }

        bool fireUpdate = false;
        int i = 0;
        private void OnPointerExited1(object sender, PointerRoutedEventArgs e)
        {
            i++;
            if (fireUpdate)
                ((HueClientViewModel)this.DataContext).UpdateLight1.Execute(null);
            fireUpdate = false;
        }

        private void OnPointerExited2(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            if(fireUpdate)
                _viewModel.UpdateLight2.Execute(null);
            fireUpdate = false;
        }

        private void OnPointerExited3(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            if(fireUpdate)
                _viewModel.UpdateLight3.Execute(null);
            fireUpdate = false;
        }

        private void OnValueChanged(object sender, Windows.UI.Xaml.Controls.Primitives.RangeBaseValueChangedEventArgs e)
        {
            fireUpdate = true;
        }

        private async void PlayEncounters(object sender, RoutedEventArgs e)
        {
            _viewModel.AnimateViewModel.SetCloseEncounters();
            var package = Windows.ApplicationModel.Package.Current;
            var installedLocation = package.InstalledLocation;
            var storageFile = await installedLocation.GetFileAsync("Assets\\Audio\\tone 3.wma");
            if (storageFile != null)
            {
                var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
                MediaElement music = new MediaElement();
                music.SetSource(stream, storageFile.ContentType);
                _viewModel.AnimateViewModel.AnimateLights.Execute(null);
                music.Play();
            }
        }
    }
}
